package com.owen.service;

import cn.hutool.core.bean.BeanUtil;
import com.owen.dao.PhoneDao;
import com.owen.entity.Phone;
import com.owen.util.FtlExcelUtil;
import com.owen.util.MyExcelUtil;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PhoneService {

    @Autowired
    private PhoneDao phoneDao;

    public void excelXMLExport(HttpServletResponse response) {
        List<Phone> phones = phoneDao.findAllPhone();
        // 导出excel
        Map<String, Object> map = new HashMap<>();
        map.put("data", phones);
        try {
            FtlExcelUtil.downloadExcel("/excelTemplates/phoneTemplate.ftl", "电话号码台账", map, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        }
    }

    public void excelXlsxExport(HttpServletResponse response) {
        List<Phone> phones = phoneDao.findAllPhone();
        MyExcelUtil.exportExcel(Phone.class, phones, "电话号码", response);
    }

}
